program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement=^element;
  element=record
    value1:integer;
    value2:integer;
    next:pelement;
  end;
var
  a: element;
  i, t1, t2, n, m: integer;
  head1, head2: pelement;
procedure AddToBegin(var head: pelement; num1, num2: integer);
var
  tmp: pelement;
begin
  tmp:=head;
  new (head);
  head^.next:=tmp;
  head^.value1:=num1;
  head^.value2:=num2;
end;
procedure NevedomayaFignya(var head1, head2: pelement);
var
  current1, current2: pelement;
  tmp: integer;
begin
  current1:=head1;
  current2:=head2;
  while (current1<>nil) and (current2<>nil) do
  begin
    if (current1^.next=nil) or (current2^.next=nil) then
      break;
    if (current1^.value2>current2^.value2) then
    begin
      writeln(current1^.value1,current1^.value2);
      current1:=current1^.next;
    end;
    if (current2^.value2>current1^.value2) then
    begin
      writeln(current2^.value1,current2^.value2);
      current2:=current2^.next;
    end;
    if (current1^.value2=current2^.value2) then
    begin
      tmp:=current1^.value1+current2^.value1;
      writeln(tmp,current1^.value2);
    end;
  end;
  if (current1^.next=nil) then
  begin
    while (current2^.next<>nil) do
    begin
      writeln(current2.value1,current2.value2);
      current2:=current2.next;
    end;
  end;
  if (current2^.next=nil) then
  begin
    while (current1^.next<>nil) do
    begin
      writeln(current1.value1,current1.value2);
      current1:=current1.next;
    end;
  end;
end;
begin
  readln(n,m);
  head1:=nil;
  head2:=nil;
  t1:=0;
  t2:=0;
  for i:=1 to n do
  begin
    readln(t1,t2);
    AddToBegin(head1,t1,t2);
  end;
  for i:=1 to n do
  begin
    readln(t1,t2);
    AddToBegin(head2,t1,t2);
  end;
  NevedomayaFignya(head1,head2);
  readln;
end.
